package com.xmq.webdemo;

import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private final String TAG = MainActivity.class.getSimpleName();
    private Button mSearchDev;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
                .penaltyLog()
                .build());
        mSearchDev = findViewById(R.id.send_udp_msg);
        mSearchDev.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send_udp_msg:
                sendData();
                break;
        }
    }


    public void sendData() {
        int controllerPort = 60000; //60000;

        int sn = 153165108;
        try {
            byte[] byteCmd = new byte[]{(byte) 0x17, (byte) 0x94,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
                    (byte) 0x00, (byte) 0x00};

//            byteCmd[0] = (byte) 0x17;
//        byteCmd[1] = (byte) 0x94;

//            byteCmd[4] = (byte) (sn & 0xff);
//        Log.i(TAG,"进行与操作后获取的值是:"+byteCmd[4]);
//            byteCmd[5] = (byte) ((sn >> 8) & 0xff);
//            byteCmd[6] = (byte) ((sn >> 16) & 0xff);
//            byteCmd[7] = (byte) ((sn >> 24) & 0xff);
//            byteCmd[8] = (byte) 1;
//            byteCmd[28] = 0x00;  //[模拟刷卡的操作] =0x00 表示进门的请求 =0x01 表示出门的请求
//            byteCmd[32] = 0x5A;
//
//            int tag = 0x70000004;
//            byteCmd[40] = (byte) (tag & 0xff);
//            byteCmd[41] = (byte) ((tag >> 8) & 0xff);
//            byteCmd[42] = (byte) ((tag >> 16) & 0xff);
//            byteCmd[43] = (byte) ((tag >> 24) & 0xff);
            byte[] content = null;
            DatagramSocket dataSocket = null;

            dataSocket = new DatagramSocket();
            dataSocket.setSoTimeout(1000);
            DatagramPacket snddataPacket = new DatagramPacket((byteCmd), byteCmd.length,
                    InetAddress.getByName("255.255.255.255"), controllerPort);
            dataSocket.send(snddataPacket);

            byte recvDataByte[] = new byte[64];

            Thread.sleep(500);
            DatagramPacket dataPacket = new DatagramPacket(recvDataByte,
                    recvDataByte.length);
            dataSocket.receive(dataPacket);

            content = dataPacket.getData();
            String s = bytes2HexString(content);
            String s1 = new String(content);

            Log.i(TAG, "接收到的消息长度是:" + s);


            dataSocket.close();


        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }



    }


    public final char HEX_DIGITS[] =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    public String bytes2HexString ( final byte[] bytes){
        if (bytes == null)
            return "";
        int len = bytes.length;
        if (len <= 0)
            return "";
        char[] ret = new char[len << 1];
        for (int i = 0, j = 0; i < len; i++) {
            ret[j++] = HEX_DIGITS[bytes[i] >> 4 & 0x0f];
            ret[j++] = HEX_DIGITS[bytes[i] & 0x0f];
        }
        return new String(ret);
    }


}
